package com.gitee.huanminabc.fastboot.orm.aop;

import com.gitee.huanminabc.fastboot.annotation.Aop;
import com.gitee.huanminabc.fastboot.orm.annotation.DB;
import com.gitee.huanminabc.fastboot.proxy.InterceptorHandle;
import com.gitee.huanminabc.jlambda_sql.jdbc.JdbcManage;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * 简要描述
 *
 * @Author: huanmin
 * @Date: 2025/4/5 20:54
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
@Aop(DB.class)
public class DbSwitchAop implements InterceptorHandle {

    @Override
    public void before(Object target,  Class<?> realClass, Method method, Object[] args, Map<String, Object> context) {
        context.put("oldDbKey", JdbcManage.getDbKey());
        DB annotation = realClass.getAnnotation(DB.class);
        JdbcManage.toggleDbKey(annotation.value());
    }

    @Override
    public void returnAfter(Object target,  Class<?> realClass, Method method, Object[] args,Map<String, Object> context) {
        String oldDbKey = (String) context.get("oldDbKey");
        if (oldDbKey != null) {
            JdbcManage.toggleDbKey(oldDbKey);
        }
    }
}
